All files / src/app/api/participants/[id]/credentials route.ts

95.65% Statements 22/23
50% Branches 9/18
100% Functions 3/3
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131        1x                                                                                 1x 1x                                                 4x 4x   4x 4x 4x       3x   3x 3x       3x 1x                 2x 2x     1x 3x             1x 1x                     3x   1x 1x            
import { NextRequest, NextResponse } from "next/server";
import { edcClient } from "@/lib/edc";
import { requireAuth, isAuthError } from "@/lib/auth-guard";
 
export const dynamic = "force-dynamic";
 
interface ParticipantProfile {
  id: string;
  identifier?: string;
  error?: boolean;
  properties?: {
    "cfm.vpa.state"?: {
      participantContextId?: string;
    };
  };
}
 
interface VerifiableCredential {
  id?: string;
  type?: string[];
  issuer?: string;
  issuanceDate?: string;
  expirationDate?: string;
  credentialSubject?: Record<string, unknown>;
  credentialStatus?: { type: string; status: string };
}
 
/** Raw IH credential resource — the actual VC is nested deeper */
interface IHCredentialResource {
  id?: string;
  state?: number;
  verifiableCredential?: {
    credential?: {
      id?: string;
      type?: string[];
      issuer?: { id?: string };
      issuanceDate?: string;
      expirationDate?: string;
      credentialSubject?: Array<Record<string, unknown>>;
      credentialStatus?: Array<{ type?: string; statusPurpose?: string }>;
    };
  };
}
 
function toVC(r: IHCredentialResource): VerifiableCredential {
  const c = r.verifiableCredential?.credential;
  return {
    id: c?.id ?? r.id,
    type: c?.type,
    issuer: c?.issuer?.id,
    issuanceDate: c?.issuanceDate,
    expirationDate: c?.expirationDate,
    credentialSubject: c?.credentialSubject?.[0],
    credentialStatus: c?.credentialStatus?.[0]
      ? {
          type: c.credentialStatus[0].type ?? "",
          status: c.credentialStatus[0].statusPurpose ?? "",
        }
      : undefined,
  };
}
 
/**
 * GET /api/participants/[id]/credentials
 * Fetches verifiable credentials from IdentityHub for every participant
 * context associated with this tenant.
 */
export async function GET(
  _req: NextRequest,
  { params }: { params: Promise<{ id: string }> },
) {
  const auth = await requireAuth();
  Iif (isAuthError(auth)) return auth;
 
  try {
    const { id } = await params;
    const profiles = await edcClient.tenant<ParticipantProfile[]>(
      `/v1alpha1/tenants/${id}/participant-profiles`,
    );
 
    const results = await Promise.all(
      profiles.map(async (pp) => {
        const ctxId = pp.properties?.["cfm.vpa.state"]?.participantContextId;
        const did = pp.identifier
          ? decodeURIComponent(pp.identifier)
          : undefined;
 
        if (!ctxId) {
          return {
            profileId: pp.id,
            participantContextId: null,
            did,
            credentials: [],
            error: "No participant context ID",
          };
        }
 
        try {
          const data = await edcClient.identity<IHCredentialResource[]>(
            `/v1alpha/participants/${ctxId}/credentials`,
          );
          const vcs = Array.isArray(data) ? data.map(toVC) : [];
          return {
            profileId: pp.id,
            participantContextId: ctxId,
            did,
            credentials: vcs,
          };
        } catch (err) {
          const msg = err instanceof Error ? err.message : String(err);
          return {
            profileId: pp.id,
            participantContextId: ctxId,
            did,
            credentials: [],
            error: msg,
          };
        }
      }),
    );
 
    return NextResponse.json(results);
  } catch (err) {
    console.error("Failed to get credentials:", err);
    return NextResponse.json(
      { error: "Failed to get credentials" },
      { status: 502 },
    );
  }
}